import {state_name_file_list} from '../../Const'
import IndexService from "../../service/IndexService";
import arrayUtil from "../../basic/utils/arrayUtil";

const initState = {
    fileList: [],
    openList: [],
    zTreeObj: null,
    v: 0
};

//模板3
const __tabItem = {
    id: 1,
    name: '',
    extName: '',
    openTime: new Date(),
    isActive: true,
    isSave: false,
    contentId: '',
    content: ''
};
const indexService = new IndexService();

const fun = {
    moveIndexForOpenList(preState, data) {
        let {dragMyIndex, dragTargetIndex} = data;
        let {openList} = preState;
        let dragMyTab = openList[dragMyIndex];
        openList.splice(dragMyIndex, 1)
        openList.splice(dragTargetIndex, 0, dragMyTab);

        preState.openList = openList;
        return preState;
    },
    //选中某个打开的tab
    selectedOpenItem(preState, item) {
        let {openList} = preState;
        openList.map(iItem => {
            iItem.isActive = iItem._id === item._id;
        })
        preState.openList = openList;
        return preState;
    },
    //关闭某个打开的tab
    closeOpenItem(preState, data) {
        let {item, index} = data;
        let {openList} = preState;
        openList.splice(index, 1);
        if (item.isActive) {
            //下一个选中
            let selectTab = openList[index] || openList[index - 1];
            if (selectTab) {
                selectTab.isActive = true;
            }
        }
        preState.openList = openList;
        return preState;
    },
    //打开一个文件
    openFile(preState, item) {
        let tabList = preState.openList;
        let isExist = false;
        tabList.map(mItem => {
            mItem.isActive = mItem._id === item._id;
            if (mItem._id === item._id) {
                isExist = true;
            }
            return mItem;
        })
        if (!isExist) {
            let newItem = Object.assign({}, this.__tabItem, item);
            newItem.isActive = true;
            tabList.push(newItem);
            let cItem = indexService.findFileContentByCId(newItem.contentId);
            newItem.content = cItem.content;
        }
        return preState;
    },
    //设置文件List
    setFileList(preState, data) {
        preState.fileList = data;
        return preState;
    },
    //增加一个文件
    addNode(preState, data) {
        preState.fileList.push(data)
        return preState;
    },
    //修改
    updateNode(preState, data) {
        let {fileList, openList} = preState;
        fileList.map(item => {
            if (item._id == data._id) {
                item = Object.assign({}, item, data);
            }
            return item;
        })
        let nOpenList = [];
        openList.map(item => {
            if (item._id == data._id) {
                item = Object.assign({}, item, data);
            }
            nOpenList.push(item);
            return item;
        })
        preState.openList = nOpenList;
        preState.fileList = Object.assign([], fileList);
        return preState;
    },
    deleteNode(preState, id) {
        let {fileList, openList} = preState;
        for (let i in fileList) {
            let item = fileList[i];
            if (item._id == id) {
                fileList.splice(i, 1);
                break;
            }
        }
        for (let i in openList) {
            let item = openList[i];
            if (item._id == id) {
                openList.splice(i, 1);
                if (item.isActive) {
                    //下一个选中
                    let selectTab = openList[i] || openList[i - 1];
                    if (selectTab) {
                        selectTab.isActive = true;
                    }
                }
                break;
            }
        }
        preState.fileList = fileList;
        preState.openList = openList;
        return preState;
    },
    setZtreeObject(preState, ztreeObj) {
        preState.zTreeObj = ztreeObj;
        return preState;
    }
}
export default function fileListReducer(preState = initState, action) {
    const {type, data} = action;
    preState.v++;
    if (fun[type]) {
        return Object.assign({}, fun[type](preState, data));
    } else {
        return preState;
    }
}
